package com.xuelang.pipeline.infrastructure.task.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.task.dto.TaskDefQuery;
import com.xuelang.pipeline.domain.task.TaskDef;
import com.xuelang.pipeline.domain.task.repository.TaskDefRepository;
import com.xuelang.pipeline.infrastructure.task.converter.TaskDefToTaskDefDOConverter;
import com.xuelang.pipeline.infrastructure.task.dataobject.TaskDefDO;
import com.xuelang.pipeline.infrastructure.task.mapper.TaskDefMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 流水线任务定义仓库实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@Repository
public class TaskDefRepositoryImpl implements TaskDefRepository {

    @Autowired
    TaskDefMapper taskDefMapper;

    @Override
    public PageRecord<TaskDef> pageList(long currentPage, long pageSize, TaskDefQuery taskDefQuery) {
        currentPage = currentPage < 1 ? 1 : currentPage;
        pageSize = pageSize < 1 ? 10 : pageSize;
        Page<TaskDef> page = new Page<>(currentPage, pageSize);
        MPJLambdaWrapper<TaskDefDO> wrapper = new MPJLambdaWrapper<TaskDefDO>();
        if (taskDefQuery != null) {
            wrapper.selectAll(TaskDefDO.class)
                    .and(StrUtil.isNotBlank(taskDefQuery.getCondition()), wp -> wp
                            .like(TaskDefDO::getName, taskDefQuery.getCondition()).or()
                            .like(TaskDefDO::getRefTag, taskDefQuery.getCondition()).or()
                            .like(TaskDefDO::getTaskDefType, taskDefQuery.getCondition()))
                    .apply(StrUtil.isNotBlank(taskDefQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')>={0}", taskDefQuery.getStartDate())
                    .apply(StrUtil.isNotBlank(taskDefQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')<={0}", taskDefQuery.getEndDate())
                    .orderByDesc(TaskDefDO::getCreateTime);
        }
        taskDefMapper.selectJoinPage(page, TaskDef.class, wrapper);
        return new PageRecord<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.getRecords());
    }

    @Override
    public List<TaskDef> list(TaskDefQuery taskDefQuery) {
        MPJLambdaWrapper<TaskDefDO> wrapper = new MPJLambdaWrapper<TaskDefDO>();
        if (taskDefQuery != null) {
            wrapper.selectAll(TaskDefDO.class)
                    .and(StrUtil.isNotBlank(taskDefQuery.getCondition()), wp -> wp
                            .like(TaskDefDO::getName, taskDefQuery.getCondition()).or()
                            .like(TaskDefDO::getRefTag, taskDefQuery.getCondition()).or()
                            .like(TaskDefDO::getTaskDefType, taskDefQuery.getCondition()))
                    .apply(StrUtil.isNotBlank(taskDefQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')>={0}", taskDefQuery.getStartDate())
                    .apply(StrUtil.isNotBlank(taskDefQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')<={0}", taskDefQuery.getEndDate())
                    .orderByDesc(TaskDefDO::getCreateTime);
        }
        return taskDefMapper.selectJoinList(TaskDef.class, wrapper);
    }

    @Override
    public Boolean save(TaskDef taskDef) {
        return taskDefMapper.insert(TaskDefToTaskDefDOConverter.INSTANCE.taskDefToTaskDefDO(taskDef)) == 1;
    }

    @Override
    public Boolean update(TaskDef taskDef) {
        return taskDefMapper.update(
                TaskDefToTaskDefDOConverter.INSTANCE.taskDefToTaskDefDO(taskDef),
                Wrappers.lambdaQuery(TaskDefDO.class)
                        .eq(TaskDefDO::getUuid, taskDef.getUuid())) == 1;
    }

    @Override
    public Boolean delete(String uuid) {
        return taskDefMapper.delete(Wrappers.lambdaQuery(TaskDefDO.class)
                .eq(TaskDefDO::getUuid, uuid)) == 1;
    }

    @Override
    public TaskDef query(String uuid) {
        TaskDefDO taskDefDO = taskDefMapper.selectOne(Wrappers.lambdaQuery(TaskDefDO.class).eq(TaskDefDO::getUuid, uuid));
        return TaskDefToTaskDefDOConverter.INSTANCE.taskDefDOToTaskDef(taskDefDO);
    }

    @Override
    public Boolean existsCode(String refTag) {
        return taskDefMapper.exists(Wrappers.lambdaQuery(TaskDefDO.class).eq(TaskDefDO::getRefTag, refTag));
    }

    @Override
    public Boolean existsName(String taskDefName) {
        return taskDefMapper.exists(Wrappers.lambdaQuery(TaskDefDO.class).eq(TaskDefDO::getName, taskDefName));
    }
}
